﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Licenta1.Shapes
{
    class Cube:Shape
    {
        public double x, y, z, length, r, g, b, reflectivity = -1;

        Expression parameters;
        bool init = false;

        public Cube(Context con, Expression expr)
            : base(con)
        {
            parameters = expr;
        }

        private void CheckParameters()
        {
            try
            {
                ParameterList p_list = (ParameterList)parameters;

                x = (double)p_list.FirstValue;
                p_list = (ParameterList)p_list.SecondValue;
                y = (double)p_list.FirstValue;
                p_list = (ParameterList)p_list.SecondValue;
                z = (double)p_list.FirstValue;
                p_list = (ParameterList)p_list.SecondValue;
                length = (double)p_list.FirstValue;
                p_list = (ParameterList)p_list.SecondValue;
                r = (double)p_list.FirstValue;
                p_list = (ParameterList)p_list.SecondValue;
                g = (double)p_list.FirstValue;

                if (p_list.SecondValue is ParameterList)
                {
                    p_list = (ParameterList)p_list.SecondValue;
                    b = (double)p_list.FirstValue;
                    reflectivity = (double)p_list.SecondValue;
                }
                else
                    b = (double)p_list.SecondValue;
            }
            catch (FormatException)
            {
                Console.WriteLine("Error handling parameters to cube.");
            }
        }

        public override string Display()
        {
            if (!init)
                CheckParameters();
            init = true;

            return "{Cube: center(" + x + ", " + y + ", " + z + "); edge = " + length +
                    "; rgb = (" + r + "; " + g + "; " + b + "); reflectivity = " + reflectivity + " }";
        }

        public override object Value
        {
            get
            {
                if (!init)
                    CheckParameters();
                return this;
            }
        }

        public override void Draw()
        {
            if (!init)
                CheckParameters();
            if (reflectivity == -1)
                context.mp.rt.AddObject(new Licenta1.RayTracer.MathCube(new Licenta1.RayTracer.Vector(x, y, z), length),
                                                                          new Licenta1.RayTracer.SolidColorMaterial(r, g, b));
            else
                context.mp.rt.AddObject(new Licenta1.RayTracer.MathCube(new Licenta1.RayTracer.Vector(x, y, z), length),
                                                                          new Licenta1.RayTracer.SolidColorMaterial(r, g, b, reflectivity));
        }
    }
}
